﻿using System.Collections.Generic;
using AutoMapper;
using BusinessLogic.Encryption;
using BusinessObjects.Entities;
using Data.DataAccessObjects;
using DataContracts.DataTransferObjects;

namespace BusinessLogic
{
    public static class PersonLogic
    {
        static PersonLogic()
        {
            InitializeMaps();
        }

        public static void InitializeMaps()
        {
            Mapper.CreateMap<PersonEntity, PersonDto>().ForMember(dest => dest.Id,
                                                                  opts =>
                                                                  opts.MapFrom(
                                                                      src => DesCrypt.EncryptId(src.Id, "PersonId")));
            Mapper.CreateMap<AddressEntity, AddressDto>().ForMember(dest => dest.Id,
                                                                    opts =>
                                                                    opts.MapFrom(
                                                                        src => DesCrypt.EncryptId(src.Id, "AddressId")));
            Mapper.AssertConfigurationIsValid();
        }

        private static readonly IPersonDao PersonDao = DaoFactory.GetPersonDao;

        public static PersonDto[] GetAll()
        {
            return Mapper.Map<IList<PersonEntity>, PersonDto[]>(PersonDao.GetAll());
        }

        public static PersonDto[] GetById(string id)
        {
            return new[] { Mapper.Map<PersonEntity, PersonDto>(PersonDao.GetById(DesCrypt.DecryptId<int>(id))) };
        }
    }
}
